import { reactive } from 'vue'
import { getTableDetailReq } from '@/service/modules/data-manage'
import { TableInfo } from '@/service/modules/data-manage/types'

export function useTable() {
  const state = reactive({
    datasourceType: null,
    datasourceId: null,
    dbName: null,
    tableName: null,
    tableDetail: {}
  })

  const getTableDetail = async () => {
    const res = await getTableDetailReq({
      datasourceId: state.datasourceId,
      dbName: state.dbName,
      tableName: state.tableName
    })
    if (res.resp_code === 0) {
      state.tableDetail = res.data as TableInfo
      state.previewData = JSON.parse(state.tableDetail.previewData)
      state.tableDetail?.columns.forEach((item) => {
        item.previewData = state.previewData[item.columnName]
      })
    }
  }

  return {
    state,
    getTableDetail
  }
}
